package com.zkingsoft.actions.visitor;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.instrument.classloading.ShadowingClassLoader;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.zkingsoft.actions.common.BaseController;
import com.zkingsoft.config.Dictionary;
import com.zkingsoft.model.Message;
import com.zkingsoft.model.Nav;
import com.zkingsoft.model.PaginationVO;
import com.zkingsoft.model.ShowBox;
import com.zkingsoft.model.ShowElement;
import com.zkingsoft.model.ShowElementCate;
import com.zkingsoft.model.ShowPoint;
import com.zkingsoft.services.FunctionService;
import com.zkingsoft.services.MessageService;
import com.zkingsoft.services.NavService;
import com.zkingsoft.services.RolePwoerService;
import com.zkingsoft.services.ShowBoxService;
import com.zkingsoft.services.ShowElementCateService;
import com.zkingsoft.services.ShowElementService;
import com.zkingsoft.services.ShowPointService;
import com.zkingsoft.services.UsersService;

/**
 * @author Matrix-J E-mail:<935090232@qq.com>
 * @date 2015年9月29日10:31
 * @description 通用控制器,本action未经过验证器
 */
@RequestMapping(value = "/visitor")
@Controller
public class VisitorController extends BaseController {

	private static final Logger log = Logger.getLogger(VisitorController.class);
	@Resource
	private UsersService usersService;
	@Resource
	private FunctionService functionService;
	@Resource
	private RolePwoerService rolePwoerService;

	@Resource
	private NavService navService;
	@Resource
	private ShowPointService showPointService;

	@Resource
	private ShowBoxService showBoxService;

	@Resource
	private ShowElementService showElementService;

	@Resource
	private ShowElementCateService showElementCateService;
	@Resource
	private MessageService messageService;

	ShowElement showElementQuery = new ShowElement();
	ShowElementCate showElementCateQuery = new ShowElementCate();

	ShowBox showboxQuery = new ShowBox();
	ShowPoint showPointQuery = new ShowPoint();

	/**
	 * 页面定向方法，每个权限模块公用一个，每个模块共享一个一级路径，已便于进行权限过滤
	 * 
	 * @param page
	 * @return
	 */
	@RequestMapping(value = "/redirect/{page1}/{page2}")
	public String redirect(@PathVariable("page1") String page1, @PathVariable("page2") String page2) {
		log.info("visitor[redirect]**********************=" + page1 + "/" + page2);
		return "visitor/" + page1 + "/" + page2;
	}

	@RequestMapping(value = "/redirect/{page1}")
	public String redirect(@PathVariable("page1") String page1) {
		log.info("visitor[redirect]**********************=" + page1);
		return "visitor/" + page1;
	}

	/**
	 * 进入首页
	 * 
	 * @param page1
	 * @return
	 */
	@RequestMapping(value = "/index")
	public String index(HttpServletRequest request) {

		checkNavInSession(request);

		request.setAttribute("banners", getShowPoint("首页banner", request));
		request.setAttribute("yqlj", getShowPoint("友情链接", request));
		request.setAttribute("zxdt", getShowPoint("最新动态", request));
		request.setAttribute("qyxw", getShowPoint("企业新闻", request));
		request.setAttribute("qywh", getShowPoint("企业文化", request));
		request.setAttribute("cptj", getShowPoint("产品推荐", request));
		request.setAttribute("sydbgg", getShowPoint("首页底部广告", request));
		return "visitor/index";
	}

	/**
	 * 进入关于麦虹
	 * 
	 * @param page1
	 * @return
	 */
	@RequestMapping(value = "/aboutus")
	public String aboutus(HttpServletRequest request, Long id) {
		checkNavInSession(request);
		List<ShowElement> artList = getCateArticle("关于我们", request);
		ShowElement obj = new ShowElement();
		if (id != null) {
			obj = showElementService.findById(id);
		} else {
			obj = artList.get(0);
		}
		request.setAttribute("artList", artList);
		request.setAttribute("obj", obj);
		return "visitor/aboutus";
	}

	/**
	 * 进入服务于支持
	 * 
	 * @param page1
	 * @return
	 */
	@RequestMapping(value = "/service")
	public String service(HttpServletRequest request, Long id) {
		checkNavInSession(request);
		List<ShowElement> artList = getCateArticle("服务与支持", request);
		ShowElement obj = new ShowElement();
		if (id != null) {
			obj = showElementService.findById(id);
		} else {
			obj = artList.get(0);
		}
		request.setAttribute("artList", artList);
		request.setAttribute("obj", obj);
		return "visitor/service";
	}
	
	
	/**
	 * 进入企业文化
	 * 
	 * @param page1
	 * @return
	 */
	@RequestMapping(value = "/qywh")
	public String qywh(HttpServletRequest request, Long id) {
		checkNavInSession(request);
		List<ShowElement> artList = getCateArticle("企业文化", request);
		ShowElement obj = new ShowElement();
		if (id != null) {
			obj = showElementService.findById(id);
		} else {
			obj = artList.get(0);
		}
		request.setAttribute("artList", artList);
		request.setAttribute("obj", obj);
		return "visitor/qywh";
	}

	
	

	/**
	 * 进入线上商城
	 * 
	 * @param page1
	 * @return
	 */
	@RequestMapping(value = "/shop")
	public String shop(HttpServletRequest request, Long id) {
		checkNavInSession(request);
		List<ShowElement> artList = getCateArticle("线上商城", request);
		ShowElement obj = new ShowElement();
		if (id != null) {
			obj = showElementService.findById(id);
		} else {
			obj = artList.get(0);
		}
		request.setAttribute("artList", artList);
		request.setAttribute("obj", obj);
		return "visitor/shop";
	}
	
	/**
	 * 进入招商加盟
	 * 
	 * @param page1
	 * @return
	 */
	@RequestMapping(value = "/investment")
	public String investment(HttpServletRequest request, Long id) {
		checkNavInSession(request);
		List<ShowElement> artList = getCateArticle("招商加盟", request);
		ShowElement obj = new ShowElement();
		if (id != null) {
			obj = showElementService.findById(id);
		} else {
			obj = artList.get(0);
		}
		request.setAttribute("artList", artList);
		request.setAttribute("obj", obj);
		return "visitor/investment";
	}
	

	/**
	 * 进入新闻中心
	 * 
	 * @param page1
	 * @return
	 */
	@RequestMapping(value = "/news")
	public String news(HttpServletRequest request, Long cateId, Long artId,Integer pageNow) {
		checkNavInSession(request);
		showElementCateQuery = new ShowElementCate();
		ShowElementCate cate = new ShowElementCate();
		List<ShowElementCate> cateList;
		showElementCateQuery.setCompanyId(Dictionary.CURRENT_COMPANY);
		showElementCateQuery.setName("新闻资讯");
		cateList = showElementCateService.findByModel(showElementCateQuery);
		showElementCateQuery = new ShowElementCate();
		showElementCateQuery.setParentId(cateList.get(0).getId());
		cateList = showElementCateService.findByModel(showElementCateQuery);
				
		if (cateId == null) {
			cate = cateList.get(0);
		} else {
			cate = showElementCateService.findById(cateId);
		}
		ShowElement art = null;
		List<ShowElement> artList = null;
		if (artId == null) {
			// 显示文章列表
			showElementQuery.setCompanyId(Dictionary.CURRENT_COMPANY);
			showElementQuery.setCateId(cate.getId());
			PaginationVO pageVo=new PaginationVO();
			if(pageNow!=null){
				pageVo.setPageNow(pageNow);
			}
			artList = showElementService.findInPage(showElementQuery, pageVo);
			
			//设置分页信息
			pageVo.setTotalRecold(showElementService.findTotal(showElementQuery));
			pageVo.setUrl("visitor/news?cateId="+cate.getId());
			
			request.setAttribute("pageVo", pageVo);
			request.setAttribute("artList", artList);
		} else {
			// 显示文章
			art = showElementService.findById(artId);
			request.setAttribute("art", art);
		}
		request.setAttribute("cate", cate);
		request.setAttribute("cateList", cateList);
		return "visitor/news";
	}

	/**
	 * 进入产品中心
	 * 
	 * @param page1
	 * @return
	 */
	@RequestMapping(value = "/pruduct")
	public String pruduct(HttpServletRequest request, Long cateId) {
		checkNavInSession(request);
		ShowElementCate cate=null;
		showElementCateQuery=new ShowElementCate();
		showElementCateQuery.setCompanyId(Dictionary.CURRENT_COMPANY);
		showElementCateQuery.setName("产品类型");
		
		List<ShowElementCate> cateList=	showElementCateService.findByModel(showElementCateQuery);
		showElementCateQuery=new ShowElementCate();
		showElementCateQuery.setParentId(cateList.get(0).getId());
		cateList=	showElementCateService.findByModel(showElementCateQuery);
		
		
		if(cateId==null){
			cate=cateList.get(0);
		}else{
			cate=showElementCateService.findById(cateId);
		}
		
		showElementQuery=new ShowElement();
		showElementQuery.setCompanyId(Dictionary.CURRENT_COMPANY);
		showElementQuery.setCateId(cate.getId());
	    List<ShowElement> pruductList=showElementService.findByModel(showElementQuery);
	    
	    request.setAttribute("cate", cate);
	    request.setAttribute("cateList", cateList);
	    request.setAttribute("pruductList", pruductList);
		return "visitor/pruduct";
	}

	/**
	 * 进入联系我们
	 * 
	 * @param page1
	 * @return
	 */
	@RequestMapping(value = "/connect")
	public String aboutus(HttpServletRequest request) {
		checkNavInSession(request);
		ShowElement obj = new ShowElement();
		showElementQuery = new ShowElement();
		showElementQuery.setName("联系热线");
		showElementQuery.setCompanyId(Dictionary.CURRENT_COMPANY);
		List<ShowElement> artList = showElementService.findByModel(showElementQuery);
		if (artList != null && artList.size() > 0) {
			obj = artList.get(0);
		}
		request.setAttribute("obj", obj);
		return "visitor/connect";
	}

	/**
	 * 进入添加留言
	 * 
	 * @param page1
	 * @return
	 */
	@RequestMapping(value = "/message")
	public String message(HttpServletRequest request) {
		checkNavInSession(request);
		return "visitor/message";
	}

	/**
	 * 添加留言
	 * 
	 * @param page1
	 * @return
	 */
	@RequestMapping(value = "/addMessage")
	public String addMessage(HttpServletRequest request, Message message) {
		message.setTime(new Date());
		message.setCompanyId(Dictionary.CURRENT_COMPANY);
		message.setIp(request.getRemoteAddr());
		messageService.add(message);
		request.setAttribute("msg", "感谢您的留言，我们会尽快处理");
		return "visitor/message";
	}

	/**
	 * 根据cate的名称查询出当前公司的article 如果没有找到返回一个空的ArrayList
	 * 
	 * @param sbName
	 * @return
	 */
	private List<ShowElement> getCateArticle(String cateName, HttpServletRequest request) {
		showElementQuery = new ShowElement();
		showElementCateQuery=new ShowElementCate();
		ArrayList<ShowElement> emptyList = new ArrayList<>();
		showElementCateQuery.setName(cateName);
		showElementCateQuery.setCompanyId(Dictionary.CURRENT_COMPANY);
		List<ShowElementCate> cates = showElementCateService.findByModel(showElementCateQuery);
		if (cates != null && cates.size() > 0) {
			showElementQuery.setCateId(cates.get(0).getId());
			showElementQuery.setCompanyId(Dictionary.CURRENT_COMPANY);
			return showElementService.findByModel(showElementQuery);
		} else {
			return emptyList;
		}
	}

	/**
	 * 检测session中是否有navlist
	 * 
	 * @param request
	 */
	private void checkNavInSession(HttpServletRequest request) {
		if (request.getSession().getAttribute("navList") == null) {
			Nav navQuery = new Nav();
			navQuery.setGrade(1);
			navQuery.setCompanyId(Dictionary.CURRENT_COMPANY);
			List<Nav> navList = navService.findByModel(navQuery);
			request.getSession().setAttribute("navList", navList);
		}
	}

	/**
	 * 根据showbox的名称查询出当前公司的showpoint 如果没有找到返回一个空的ArrayList
	 * 
	 * @param sbName
	 * @return
	 */
	private List<ShowPoint> getShowPoint(String sbName, HttpServletRequest request) {
		showboxQuery = new ShowBox();
		showPointQuery = new ShowPoint();
		ArrayList<ShowPoint> emptyList = new ArrayList<>();
		PaginationVO pageVo = new PaginationVO();
		showboxQuery.setName(sbName);
		showboxQuery.setCompanyId(Dictionary.CURRENT_COMPANY);
		List<ShowBox> showboxlist = showBoxService.findByModel(showboxQuery);
		if (showboxlist != null && showboxlist.size() > 0) {
			showPointQuery.setSbId(showboxlist.get(0).getId());
			showPointQuery.setCompanyId(Dictionary.CURRENT_COMPANY);
			pageVo.setPageNow(1);
			pageVo.setPageSize(showboxlist.get(0).getCapacity());
			return showPointService.findInPage(showPointQuery, pageVo);
		} else {
			return emptyList;
		}
	}

}